package com.example.zhangxuemin.fragmenttest130zxm.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zhangxuemin.fragmenttest130zxm.R;
import com.example.zhangxuemin.fragmenttest130zxm.iface.RegListener;
import com.example.zhangxuemin.fragmenttest130zxm.model.RegModel;

/**
 * Created by zhangxuemin on 2017/5/31.
 */

public class RegActivity extends AppCompatActivity implements View.OnClickListener,RegListener {
    private EditText username;
    private EditText password;
    private EditText tel;
    private EditText repassword;
    private Button btn_confirm;
    private Button btn_return;

    protected void onCreate(Bundle savelnstanceState){
        super.onCreate(savelnstanceState);
        setContentView(R.layout.activity_register);

        init();
        // sp=getSharedPreferences("login",MODE_PRIVATE);
        btn_confirm.setOnClickListener(this);
        btn_return.setOnClickListener(this);
    }

    private void init() {
        username=(EditText)findViewById(R.id.et_username);
        password=(EditText)findViewById(R.id.et_password);
        tel=(EditText)findViewById(R.id.et_tel);
        repassword=(EditText)findViewById(R.id.et_rePassword);
        btn_confirm=(Button)findViewById(R.id.btn_register);
        btn_return=(Button)findViewById(R.id.btn_return);
        //sp=getSharedPreferences("login",MODE_PRIVATE);//第一个参数：文件名，第二个参数：模式

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                String tmpuser = username.getText().toString();
                String tmppass = password.getText().toString();
                String tmptel = tel.getText().toString();
                String tmprepassword = repassword.getText().toString();
                RegModel regModel = new RegModel();
                regModel.reg(tmpuser, tmppass, tmptel, tmprepassword, (RegListener) this);
                break;
            case R.id.btn_return:
                Intent intent = new Intent(RegActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
        }

    }
    @Override
    public void onResponse(String s){
    if(s.equals("1")){
        Toast.makeText(RegActivity.this,"注册成功，返回登陆",Toast.LENGTH_LONG).show();
        Intent intent = new Intent(RegActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    } else

            Toast.makeText(RegActivity.this,"用户已经存在",Toast.LENGTH_LONG).show();


}

    @Override
    public void onFail (String msg){
        Toast.makeText(RegActivity.this, msg, Toast.LENGTH_SHORT).show();
    }
}